using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Basics
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GameTime gameTime;
        public Double deltaTime;

        private DebugPanel debugPanel;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public SpriteFont font;

        /// <summary>
        /// The main camera.
        /// </summary>
        Camera2D camera;

        /// <summary>
        /// Represents the custom cursor which is used to draw a cursor insted of the default mouse pointer.
        /// </summary>
        CustomCursor crosshair;

        // Mouse and Keyboad state variables. They're kept as global variables because calling GetState() is expensive so it's much better to call it once per Update and store it.
        /// <summary>
        /// Holds the current MouseState.
        /// </summary>
        MouseState mouseState;

        /// <summary>
        /// Holds the current keyboardState.
        /// </summary>
        KeyboardState keyboardState;

        /// <summary>
        /// Represents the human players tank.
        /// </summary>
        Tank playerTank;

        /// <summary>
        /// Hods all contents of the scene
        /// </summary>
        Scene scene;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // This causes Update to be called as often as possible instead of being called on a fixed interval.
            this.IsFixedTimeStep = false;

            // Disables V-sync.
            graphics.SynchronizeWithVerticalRetrace = false;

            // Show mouse cursor.
            this.IsMouseVisible = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Loads the default font.
            this.font = Content.Load<SpriteFont>("Fonts/Lindsey");

            // Loads the crosshair.
            this.crosshair = new CustomCursor(Content.Load<Texture2D>("Crosshairs/crosshair1"), Color.White, 0.0f, true);
            
            // TODO: use this.Content to load your game content here
            playerTank = new Tank(Content.Load<Texture2D>("Textures/Tank/tank_body"), Content.Load<Texture2D>("Textures/Tank/tank_turret"), 175.0f, new Vector2(GraphicsDevice.Viewport.Width/2, GraphicsDevice.
            Viewport.Height/2));

            // Loads a default font
            FpsMeter.Initialize(new Vector2(0, 0), this.font);

            // Loads the first map
            //MapLoader.LoadMap("Content/Maps/map1.txt", 32);

            // Calling BuildScene.
            this.BuildScene();

        }

        protected void BuildScene()
        {
            this.camera = new Camera2D(new Vector2(0, 0));
            scene = new Scene(this.camera);
            scene.AddObject(this.playerTank);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            this.mouseState = Mouse.GetState();
            this.keyboardState = Keyboard.GetState();

            this.deltaTime = gameTime.ElapsedGameTime.TotalSeconds;

            // Allows the game to exit
            if (keyboardState.IsKeyDown(Keys.Escape))
                this.Exit();
            if (keyboardState.IsKeyDown(Keys.A))
                playerTank.MoveLeft();
            if (keyboardState.IsKeyDown(Keys.D))
                playerTank.MoveRight();
            if (keyboardState.IsKeyDown(Keys.W))
                playerTank.MoveUp();
            if (keyboardState.IsKeyDown(Keys.S))
                playerTank.MoveDown();

            this.playerTank.RotateTurret(Math2.VectorsToAngle(new Vector2(this.playerTank.Position.X, this.playerTank.Position.Y), new Vector2(mouseState.X, mouseState.Y)));

            this.DebugCamera(gameTime, 100.0f  * (float)gameTime.ElapsedGameTime.TotalSeconds);

            base.Update(gameTime);
        }
        
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            scene.DrawScene(this.spriteBatch);

            base.Draw(gameTime);

            this.gameTime = gameTime;

            // Draws the FPS Meter.
            FpsMeter.Draw(gameTime);

            // Draws the crosshair.
            this.crosshair.Draw(this.spriteBatch, this.mouseState, this.camera.Position);
        }

        protected void DebugCamera(GameTime gameTime, float scale)
        {
            if(this.keyboardState.IsKeyDown(Keys.Up))
            {
                this.camera.Position = this.camera.Position + new Vector2(0, -1) * scale;
            }

            if (this.keyboardState.IsKeyDown(Keys.Down))
            {
                this.camera.Position = this.camera.Position + new Vector2(0, 1) * scale;
            }

            if (this.keyboardState.IsKeyDown(Keys.Left))
            {
                this.camera.Position = this.camera.Position + new Vector2(-1, 0) * scale;
            }

            if (this.keyboardState.IsKeyDown(Keys.Right))
            {
                this.camera.Position = this.camera.Position + new Vector2(1, 0) * scale;
            }
        }
    }
}
